package hng.controller.admin;

import hng.dto.admin.BannerQuery;
import hng.dto.admin.NavQuery;
import hng.dto.admin.StoreQuery;
import hng.dto.admin.UserQuery;
import hng.dto.store.CommentQuery;
import hng.dto.store.OrdersQuery;
import hng.dto.store.ProductQuery;
import hng.exception.BizException;
import hng.model.SysInfoDO;
import hng.service.ProductCategoryService;
import hng.service.admin.AdminService;
import hng.util.JsonData;
import hng.vo.admin.ProductCategoryVO;
import hng.vo.admin.TotalDataVO;
import hng.vo.store.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired(required = false)
    private AdminService adminService;

    @Autowired(required = false)
    private ProductCategoryService productCategoryService;

    /**
     * 分页查询所有用户
     * **/
    @PostMapping("/user/list")
    public JsonData userList (@RequestBody UserQuery userQuery) {
        PageResult pageResult = adminService.userList(userQuery);
        return JsonData.buildSuccess(pageResult);
    }

    /**
     * 分页查询管理员列表
     * **/
    @PostMapping("/user/admin")
    public JsonData userAdmin (@RequestBody UserQuery userQuery) {
        PageResult pageResult = adminService.adminList(userQuery);
        return JsonData.buildSuccess(pageResult);
    }

    /**
     * excel批量导入管理员
     */
    @PostMapping("/user/import-admin")
    public JsonData addUserList(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            throw new BizException("文件为空");
        }
        try {
            adminService.importAdmin(file);
        } catch (Exception e) {
            throw new BizException("An error occurred while processing the file: " + e);
        }
        return JsonData.buildSuccess();
    }

    /**
     * 查询商品列表
     */
    @PostMapping("/product/list")
    public JsonData productList(@RequestBody ProductQuery productQuery) {
        PageResult pageResult = adminService.productList(productQuery);
        return JsonData.buildSuccess(pageResult);
    }

    /**
     * 分页查询商品评论
     */
    @PostMapping("/comment/list")
    public JsonData commentList(@RequestBody CommentQuery commentQuery) {
        PageResult pageResult = adminService.commentList(commentQuery);
        return JsonData.buildSuccess(pageResult);
    }

    /**
     * 分页查询订单
     */
    @PostMapping("/orders/list")
    public JsonData ordersList(@RequestBody OrdersQuery ordersQuery) {
        PageResult pageResult = adminService.ordersList(ordersQuery);
        return JsonData.buildSuccess(pageResult);
    }

    /**
     * 分页查询店铺列表
     */
    @PostMapping("/store/list")
    public JsonData storeList(@RequestBody StoreQuery storeQuery) {
        PageResult pageResult = adminService.storeList(storeQuery);
        return JsonData.buildSuccess(pageResult);
    }

    /**
     * 分页查询轮播图列表
     */
    @PostMapping("/banner/list")
    public JsonData bannerList(@RequestBody BannerQuery bannerQuery) {
        PageResult pageResult = adminService.bannerList(bannerQuery);
        return JsonData.buildSuccess(pageResult);
    }

    /**
     * 分页查询导航栏列表
     */
    @PostMapping("/nav/list")
    public JsonData navList(@RequestBody NavQuery navQuery) {
        PageResult pageResult = adminService.navList(navQuery);
        return JsonData.buildSuccess(pageResult);
    }

    /**
     * 查询树形分类
     */
    @GetMapping("/category/tree")
    public JsonData categoryTree() {
        List<ProductCategoryVO> categoryVOList = productCategoryService.getAllCategories();
        return JsonData.buildSuccess(categoryVOList);
    }

    /**
     * 查询网站配置信息
     */
    @GetMapping("/sys/info")
    public JsonData sysInfo() {
        List<SysInfoDO> sysInfoDOList = adminService.sysInfo();
        return JsonData.buildSuccess(sysInfoDOList);
    }

    /**
     * 获取首页4个数据
     */
    @GetMapping("/total-data")
    public JsonData totalData() {
        TotalDataVO totalDataVO = adminService.totalData();
        return JsonData.buildSuccess(totalDataVO);
    }
}
